package main

func main() {
	removeElement([]int{0, 1, 2, 2, 3, 0, 4, 2}, 2)

}
func removeElement(nums []int, val int) int {
	i := 0
	j := 1
	for j < len(nums) {
		if nums[i] != val {
			i++
			j++
		} else {
			for j < len(nums) && nums[j] == val {
				j++
			}
			if j == len(nums) {
				break
			}
			nums[i] = nums[j]
			nums[j] = val
			i++
			j++
		}
	}
	return i - 1
}
